declare let cacheGet: any;
module MC {
	class USER {
		public constructor() {
			$(() => {
				this.upUserInfo();
			});
		}

		/**
		 * 获取当前用户信息,如果没登录,则弹出登录窗口
		 * */
		public getInfo(fun:Function,nologin:Function=null) {
			///console.log("自动编译123");
			this.Load(()=>{
				if(!this.info){
					nologin&&nologin();
					MC.login();
					return;
				}
				fun&&fun(this.info);
			});
		}

		/** 
		 * 获取用户类型
		 */
		public getType(): USERTYPE {
			return MC.USERTYPE.Type_gongsi_gongchang;

		}

		/***
		 * 本地缓存的用户信息
		*/
		get info(): any {
			let user = MC.Cache.getObj('user');
			if (!user) {
				return false;
				/*;*/
				//return window.open('/h5/login/login.html', '_self');
			} else {
				return user;
			}
		}

		/** 
		 * 判断是否登陆
		 */
		public isLogin() {
			return MC.Cache.getObj('user');
		}
		/**
		 * 绑定加载完成的事件
		 * */	
		public Load(fun:Function){
			MC.on("mc_getInfo",(event:any) => {
				fun && fun(this.info);
			});
		}


		private isLoad=false;

		/*** 获取最新的登录状态并更新用户信息 */
		public upUserInfo(fun: Function = null) {
			MC.api('getNowUserInfo', {},  (json: any)=>{
				//// /api/app/getnowuserinfo
				this.isLoad=true;
				if (json == null) {
					fun && fun(null);
					MC.Cache.setObj('user', null);
					MC.dispatchEvent("mc_getInfo");
					return;
				}
				var obj: any = {};
				obj.is_company = json.is_company;
				obj.is_factory = json.is_factory;
				obj.is_yingpin = json.is_yingpin;
				obj.avatar = json.avatar;
				obj.username = json.username;
				obj.id = json.id;
				obj.jiguang_id = json.jiguang[4];
				obj.phone = json.phone;
				///appkey: string, random_str: string, timestamp: string, signature: string
				MC.Call.JiguangInit(json.jiguang[0],json.jiguang[1],json.jiguang[2],json.jiguang[3],json.jiguang[4],json.jiguang[5]);
				MC.Cache.setObj('user', obj);
				fun && fun(obj);
				MC.dispatchEvent("mc_getInfo");
			});
		}

	}
	export let user = new USER();
	export enum USERTYPE {
		Type_yuangong = 1,////1个人用户
		Type_gongsi = 2,///2公司；
		Type_gongchang = 3,///3工厂
		Type_gongsi_gongchang = 4////4企业
	}
}